﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CuteEditor;
using Wjtour.Domain;
using Wjtour.BLL;
using Hjp.FrameWork.Common;
using System.IO;

namespace Wjtour.Web.Admin.picture
{
    public partial class galleryaddedi : BasePage
    {
        protected DropDownList cid;
        protected TextBox title;
        protected TextBox gallerydesc;
        protected Button submit;
        protected Button btnPublish;
        protected string type;
        protected CuteWebUI.Uploader Uploader1;
        protected Repeater rptPreview;

        protected int id = 0;
        Wjtour.Domain.gallery link = new Domain.gallery();
        sitepicture_bll _sitepicture_bll = new sitepicture_bll();
        picturetype_bll _tbll = new picturetype_bll();
        private static List<Wjtour.Domain.dto.sitepicture_picturetype> tempPictures = new List<Wjtour.Domain.dto.sitepicture_picturetype>();
        private gallery_bll _gallery_bll = new gallery_bll();

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Utils.StrToInt(Request["id"], 0);
            type = Request["type"];
            submit.Click += new EventHandler(submit_Click);
            Uploader1.UploadCompleted += Uploader1_UploadCompleted;
            if (!IsPostBack)
            {
                BindDropdown();
            }
            if (id > 0)
            {
                link = _gallery_bll.Load(id);
                tempPictures = _gallery_bll.GetGalleryPicturesList(id);
                bindRepeater(tempPictures);

                if (!IsPostBack)
                {
                    if (link != null)
                    {
                        title.Text = link.title;
                        cid.SelectedValue = link.picturetypeid.ToString();
                        gallerydesc.Text = link.gallerydesc;
                    }
                }
            }
        }

        public string GetCoverClass(object obj)
        {
            if (obj != null)
            {
                if (Convert.ToInt32(obj) == 1)
                {
                    return "class=\"coverbg\"";
                }
            }
            return string.Empty;
        }

        void Uploader1_UploadCompleted(object sender, CuteWebUI.UploaderEventArgs[] args)
        {
            args.ToList().ForEach(a =>
            {
                string filename = GetFileName("time");
                a.CopyTo("~/uploads/" + filename + ".jpg");
                a.Delete();
                var sp = new Wjtour.Domain.dto.sitepicture_picturetype
                {
                    picturepath = "/uploads/" + filename + ".jpg",
                    picturetitle = filename,
                    iscover = 0
                };
                var smallImg = ImageUtil.ConvertImageToProperSize(HttpContext.Current, "/uploads/" + filename + ".jpg", 218, 287, "jpg");
                sp.smallImg = smallImg;
                tempPictures.Add(sp);
                if (id > 0)
                {
                    var sitepic = new sitepicture();
                    sitepic.picturepath = sp.picturepath;
                    sitepic.picturetitle = sp.picturetitle;
                    sitepic.smallImg = sp.smallImg;
                    sitepic.createdate = DateTime.Now;
                    sitepic.iscover = 0;
                    sitepic.isactive = 1;
                    sitepic.isaudit = 2;
                    sitepic.galleryid = id;
                    sitepic.picturetypeid = link.picturetypeid;
                    _sitepicture_bll.Save(sitepic);
                }
            });
            bindRepeater(tempPictures);
        }

        void bindRepeater(object datasource)
        {
            rptPreview.DataSource = datasource;
            rptPreview.DataBind();
        }

        void BindDropdown()
        {
            string sql = "select * from picturetype";
            if (!string.IsNullOrEmpty(type))
                sql = "select * from picturetype where id in (" + type + ")";

            List<picturetype> types = _tbll.GetAll<picturetype>(sql).ToList();
            if (types != null && types.Count > 0)
            {
                List<picturetype> ptypes = types.Where(t => t.parenttypeid == 0).ToList();
                foreach (var ptype in ptypes)
                {
                    cid.Items.Add(new ListItem(ptype.picturetypename, ptype.id.ToString()));
                    List<picturetype> subtypes = types.Where(t => t.parenttypeid == ptype.id).ToList();
                    if (subtypes != null && subtypes.Count > 0)
                    {
                        foreach (var item in subtypes)
                        {
                            cid.Items.Add(new ListItem(" ┝ " + item.picturetypename, item.id.ToString()));
                        }
                    }
                }
            }
        }

        void submit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(title.Text))
            {
                Utils.Alert(this.Page, "图片标题不能为空！");
                return;
            }

            link.id = id;
            link.title = title.Text;
            link.picturetypeid = Utils.StrToInt(cid.SelectedValue, 0);
            link.gallerydesc = gallerydesc.Text;
            bool b = false;
            if (link.id == 0)
            {
                link.createdate = DateTime.Now;
                b = _gallery_bll.Save(link, out id);
                if (tempPictures.Count > 0)
                {
                    foreach (var item in tempPictures)
                    {
                        var sitepic = new sitepicture();
                        sitepic.picturepath = item.picturepath;
                        sitepic.picturetitle = item.picturetitle;
                        sitepic.smallImg = item.smallImg;
                        sitepic.createdate = DateTime.Now;
                        sitepic.iscover = 0;
                        sitepic.isactive = 1;
                        sitepic.isaudit = 2;
                        sitepic.galleryid = id;
                        sitepic.picturetypeid = link.picturetypeid;
                        _sitepicture_bll.Save(sitepic);
                    }
                }
                tempPictures.Clear();
                Utils.Redirect(this.Page, "list.aspx?type=" + type, false);
            }
            else
            {
                b = _gallery_bll.Save(link);
            }
            if (b)
            {
                SaveLog(id == 0 ? "Add" : "edit");
                Utils.Alert(this.Page, "保存成功！");
                tempPictures.Clear();
                Utils.Redirect(this.Page, "list.aspx?type=" + type, false);
            }
        }
    }
}